import { createWebHashHistory, createRouter } from 'vue-router'
import store from '../store'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/find'
    },
    {
      path: '/company',
      meta: {
        isShowTab: true
      },
      component: () => import('@/views/company/index.vue')
    },
    {
      path: '/question',
      meta: {
        isShowTab: true
      },
      component: () => import('@/views/question/index.vue')
    },
    {
      path: '/find',
      meta: {
        isShowTab: true
      },
      component: () => import('@/views/find/index.vue')
    },
    {
      path: '/my',
      meta: {
        isShowTab: true,
        isNeedLogin: true
      },
      component: () => import('@/views/my/index.vue')
    },
    {
      path: '/my/info',
      meta: {
        isShowTab: false,
        isNeedLogin: true
      },
      component: () => import('@/views/my/info.vue')
    },
    {
      path: '/my/edit',
      meta: {
        isShowTab: false,
        isNeedLogin: true
      },
      component: () => import('@/views/my/edit.vue')
    },
    {
      path: '/login',
      meta: {
        isShowTab: false
      },
      component: () => import('@/views/login/index.vue')
    }
  ]
})

// 导航守卫
const whiteList = ['/login', '/company', '/find', '/question']
router.beforeEach((to, from, next) => {
  // 报名单直接过
  if (whiteList.includes(to.path)) {
    next()
  } else {
    // 需要登录
    if (to.meta.isNeedLogin) {
      if (store.getters.getUserInfo) {
        next()
      } else {
        next('/login?redirect=' + to.path)
      }
    } else {
      // 不需要登录
      next()
    }
  }
})

export default router
